package handler

import (
	"chat/api/register"
	"chat/api/response"
	"chat/initlist/mysql"
	"chat/inits/model"
	"github.com/gin-gonic/gin"
)

func Login(r *gin.Context) {
	req := register.User{}
	if err := r.ShouldBind(&req); err != nil {
		response.ErrorResponse(r, "绑定失败")
	}

	user := register.User{}

	if user.Id > 0 {
		response.ErrorResponse(r, "用户不存在,请前往注册")
		return
	}

	mysql.DB.Debug().Where("username = ?", user.Username).Find(&user)

	if req.Password != user.Password {
		response.ErrorResponse(r, "密码错误")
		return
	}

	response.SuccessResponse(r, "登录成功", user.Id)
}

func UserList(r *gin.Context) {
	req := register.User{}
	if err := r.ShouldBind(&req); err != nil {
		response.ErrorResponse(r, "绑定失败")
	}

	var all []model.User
	mysql.DB.Debug().Find(&all)
	response.SuccessResponse(r, "好友列表查询成功", all)
}
